package com.dmwork.service.customer.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.MD5;
import com.dmwork.service.customer.entity.AdminCustomer;
import com.dmwork.service.customer.entity.Customer;
import com.dmwork.service.customer.entity.vo.AdminLoginVo;
import com.dmwork.service.customer.entity.vo.CustomerAdminBriefInfoVo;
import com.dmwork.service.customer.entity.vo.CustomerAdminDetailInfoVo;
import com.dmwork.service.customer.entity.vo.CustomerAdminQueryVo;
import com.dmwork.service.customer.service.AdminCustomerService;
import com.dmwork.service.customer.service.CustomerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Transactional
@RestController
@RequestMapping("/admin/dm_work/customer")
public class AdminCustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private AdminCustomerService adminCustomerService;

    /**
     * 登录
     *
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody AdminLoginVo adminLoginVo) {
        String encrypt = MD5.encrypt(adminLoginVo.getPassword());
        int count = adminCustomerService.count(new QueryWrapper<AdminCustomer>().eq("name", adminLoginVo.getName()).eq("password", encrypt));
        if (count>0) {
            return R.ok().data("token", adminLoginVo.getName());
        }else {
            return R.error().message("登录失败");
        }
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("info")
    public R info(@RequestParam("token") String token) {
        AdminCustomer adminCustomer = adminCustomerService.getOne(new QueryWrapper<AdminCustomer>().eq("name", token));
        return R.ok()
                .data("roles", "["+adminCustomer.getRoles()+"]")
                .data("name", adminCustomer.getName())
                .data("avatar", adminCustomer.getAvatar());
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("logout")
    public R logout() {
        return R.ok();
    }

    @ApiOperation("获取用户信息列表【可指定查询条件】")
    @GetMapping("get_info_list/{page}/{limit}")
    public R getInfoList(@PathVariable Integer page, @PathVariable Integer limit, CustomerAdminQueryVo customerAdminQueryVo) {
        Page<Customer> customerPage = customerService.getCustomerPage(page, limit, customerAdminQueryVo);
        List<CustomerAdminBriefInfoVo> customerAdminBriefInfoVoList = new ArrayList<>(limit);
        for (Customer customer : customerPage.getRecords()) {
            CustomerAdminBriefInfoVo customerAdminBriefInfoVo = new CustomerAdminBriefInfoVo();
            BeanUtils.copyProperties(customer, customerAdminBriefInfoVo);
            customerAdminBriefInfoVoList.add(customerAdminBriefInfoVo);
        }
        return R.ok().data("total", customerPage.getTotal()).data("rows", customerAdminBriefInfoVoList);
    }

    @ApiOperation("获取指定用户的信息详情")
    @GetMapping("get_info/{customerId}")
    public R getInfo(@PathVariable String customerId) {
        CustomerAdminDetailInfoVo customerAdminDetailInfoVo = customerService.getCustomerAdminDetailInfoById(customerId);
        return R.ok().data("item", customerAdminDetailInfoVo);
    }

}
